package oba;

import common.SystemVariyable;
import common.util.HibernateUtil;
import java.awt.Dimension;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JInternalFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import master.committee.REGCommittee;
import master.member.REGMember;
import master.member_type.REGmember_Type;
import master.privilage.REGPrivilage;
import org.pushingpixels.flamingo.api.common.AbstractCommandButton;
import org.pushingpixels.flamingo.api.common.JCommandButton;
import org.pushingpixels.flamingo.api.common.icon.ImageWrapperResizableIcon;
import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
import org.pushingpixels.flamingo.api.ribbon.JRibbonBand;
import org.pushingpixels.flamingo.api.ribbon.JRibbonFrame;
import org.pushingpixels.flamingo.api.ribbon.RibbonElementPriority;
import org.pushingpixels.flamingo.api.ribbon.RibbonTask;
import org.pushingpixels.flamingo.api.ribbon.resize.CoreRibbonResizePolicies;
import org.pushingpixels.flamingo.api.ribbon.resize.IconRibbonBandResizePolicy;
import org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel;
import zsystem.Resource;

/**
 *
 * @author Sasiri
 */
public class NewJFrame extends JRibbonFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
//        SystemVariyable.session = HibernateUtil.getSessionFactory().openSession();
        loadRibon();
    }

    private void loadRibon() {

        Dimension d = new Dimension(100, 110);

        this.getRibbon().setPreferredSize(d);
//        addBand("Member");
//        addButton("Members of OBA", RibbonElementPriority.TOP);
//        addButton("Members of OBA", RibbonElementPriority.TOP);
//        addButton("Members of OBA", RibbonElementPriority.TOP);
//        addButton("Members of OBA", RibbonElementPriority.TOP);
//
//        addBand("Member1");
//        addButton("Members of OBA2", RibbonElementPriority.TOP);
//        addButton("Members of OBA3", RibbonElementPriority.TOP);
//        addButton("Members of OBA3", RibbonElementPriority.TOP);
//        addButton("Members of OBA3", RibbonElementPriority.TOP);
//        addTask("Task");

//        startGroup();
        addBand("Member", Resource.getResource(Resource.MEMBERS));
        addButton("Members", Resource.MEMBERS, RibbonElementPriority.TOP, REGMember.class);
        addButton("Member Type", Resource.MEMBER_TYPE, RibbonElementPriority.TOP, REGmember_Type.class);
        addButton("Committee", Resource.COMMITTEE, RibbonElementPriority.TOP, REGCommittee.class);
        addButton("Privilage", Resource.COMMITTEE, RibbonElementPriority.TOP, REGPrivilage.class);
        addTask("Ta");

        addBand("Member11", Resource.getResource(Resource.MEMBERS));
        addButton("Members of OBA", Resource.ACCOUNT, RibbonElementPriority.MEDIUM, REGCommittee.class);
        addButton("Members of OBA", Resource.MEMBERS, RibbonElementPriority.LOW, REGMember.class);
        addTask("Task1");

//
//        addBand("Cccc");
//        band.addCommandButton(commandButton("Members of OBA2"), RibbonElementPriority.TOP);
//        band.addCommandButton(commandButton("Members of OBA3"), RibbonElementPriority.LOW);
//        band.setResizePolicies((List) Arrays.asList(new CoreRibbonResizePolicies.None(band.getControlPanel()), new IconRibbonBandResizePolicy(band.getControlPanel())));
//        band.setResizePolicies((List) Arrays.asList(new CoreRibbonResizePolicies.None(band.getControlPanel()), new IconRibbonBandResizePolicy(band.getControlPanel())));
//
//        this.getRibbon().addTask(addTask("Task2", band));
//
//        addBand("Cccc");
//        band.addCommandButton(commandButton("Members of OBA2"), RibbonElementPriority.TOP);
//        band.addCommandButton(commandButton("Members of OBA3"), RibbonElementPriority.LOW);
//        band.setResizePolicies((List) Arrays.asList(new CoreRibbonResizePolicies.None(band.getControlPanel()), new IconRibbonBandResizePolicy(band.getControlPanel())));
//        band.setResizePolicies((List) Arrays.asList(new CoreRibbonResizePolicies.None(band.getControlPanel()), new IconRibbonBandResizePolicy(band.getControlPanel())));
//
//        this.getRibbon().addTask(addTask("Task2", band));
        this.setExtendedState(MAXIMIZED_BOTH);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.PAGE_AXIS));

        jDesktopPane1.setMinimumSize(new java.awt.Dimension(880, 300));

        javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
        jDesktopPane1.setLayout(jDesktopPane1Layout);
        jDesktopPane1Layout.setHorizontalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 880, Short.MAX_VALUE)
        );
        jDesktopPane1Layout.setVerticalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 546, Short.MAX_VALUE)
        );

        getContentPane().add(jDesktopPane1);

        pack();
    }// </editor-fold>//GEN-END:initComponents
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        REGMember test = new REGMember();
//        test.add(button, gbc);
        jDesktopPane1.add(test);
        test.setVisible(true);
    }

    /**
     * ^
     *
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
//            loadRibon();
            UIManager.setLookAndFeel(new SubstanceOfficeBlue2007LookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                NewJFrame frame1 = new NewJFrame();
                frame1.setVisible(true);
                frame1.pack();

            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane jDesktopPane1;
    // End of variables declaration//GEN-END:variables
    String curTask;
    private final List<JRibbonBand> curBands = new ArrayList<>();
    private JRibbonBand curBand;

    private AbstractCommandButton commandButton(String Title, ResizableIcon icon, final Class cls) {
        JCommandButton commandButton = new JCommandButton(Title, icon);
        commandButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    if (SystemVariyable.session == null) {
                        SystemVariyable.session = HibernateUtil.getSessionFactory().openSession();
                    } else if (!SystemVariyable.session.isConnected()) {
                        SystemVariyable.session = HibernateUtil.getSessionFactory().openSession();
                    }
                    Object o = cls.newInstance();
                    JInternalFrame i = (JInternalFrame) o;
                    jDesktopPane1.add(i);
                    i.setVisible(true);
                } catch (InstantiationException | IllegalAccessException ex) {
                    Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        return commandButton;
    }

    protected void addTask(String title) {
        curTask = title;
        if (curTask != null) {
            createTask();
        }

    }

    protected void addBand(String title, URL iconURL) {
//        if (curBand != null) {
//            setResizePolicies(curBand);
//        }

        Dimension iconDim = new Dimension(22, 22);
        JRibbonBand band = new JRibbonBand(title, ImageWrapperResizableIcon.getIcon(iconURL, iconDim));

        curBands.add(band);
        curBand = band;
    }

    protected void startGroup() {
        curBand.startGroup();
    }

    private void createTask() {
//        if (curBand != null) {
//            setResizePolicies(curBand);
//        }

//        JRibbonBand[] bands = new JRibbonBand[curBands.size()];
//        int i = 0;
        for (JRibbonBand jRibbonBand : curBands) {
//            bands[i] = jRibbonBand;
            getRibbon().addTask(new RibbonTask(curTask, jRibbonBand));
//            ++i;
        }
        curBands.clear();
    }

//    private void setResizePolicies(JRibbonBand band) {
//        ArrayList policy = new ArrayList();
//        policy.add(new CoreRibbonResizePolicies.Low2Mid(band.getControlPanel()));
//        policy.add(new CoreRibbonResizePolicies.Mid2Low(band.getControlPanel()));
//        band.setResizePolicies(policy);
//    }
    protected void addButton(String title, String icon, RibbonElementPriority priority, Class cls) {
        curBand.addCommandButton(commandButton(title, Resource.getResizableIcon(icon), cls), priority);
        curBand.setResizePolicies((List) Arrays.asList(new CoreRibbonResizePolicies.None(curBand.getControlPanel()), new IconRibbonBandResizePolicy(curBand.getControlPanel())));

    }

}
